//index.js
//获取应用实例
const app = getApp()
var router = require("../../router.js");
Page({
  data: {
    login: 0,  
    content: 1, //内容
    tabBar: 0,//底部
    logo: false,     //登录框
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  start_listenlist:function(){
    app.navTo('../speaklist/speaklist')
  },
  start_energy: function () {
    app.navTo('../energylist/energylist')
  },
  start_movement: function(){
    app.navTo('../movement/movement')    
  },
  onLoad: function (options) {  
    var that = this;
    this.isLogin();
  },
  // 底部
  index: function () {
    app.radTo('../index/index')
  },
  dianzan: function () {
    app.radTo('../list/list')
  },
  my: function () {
    app.radTo('../my/my')
  },
  positive: function () {
    app.radTo('../positive/positive')
  },
  //获取微信
  getUserInfo: function (e) {
    var that = this;
    wx.showLoading({
      title: '请稍后',
      success: function () {
        wx.login({
          success: function (result) {
            wx.request({
              url: router.user.login,
              method: "POST",
              data: {
                code: result.code,
                encryptedData: e.detail.encryptedData,
                iv: e.detail.iv,
              },
              success: function (res) { 
                                        
                if(res.data.status === true){
                  wx.setStorageSync("uid", res.data.user.uid)
                  wx.setStorageSync("head", res.data.user.icon)
                  wx.setStorageSync("nickname", res.data.user.name)
                  wx.setStorageSync("sex", res.data.user.sex)
                  wx.setStorageSync("birthday", res.data.user.birthday)
                  wx.setStorageSync("introduce", res.data.user.introduce)                 
                  that.setData({                  
                      hasUserInfo: true,
                      logo: false
                  })
                  wx.hideLoading();
                  wx.showToast({
                    title: '恭喜你登陆成功',
                    duration: 2000,
                    success: function () {
                      if (that.data.userType == 1) {
                        wx.redirectTo({
                          url: '../index/index'
                        })
                      } else {
                        wx.showTabBar({})
                        setTimeout(function () {
                          wx.redirectTo({
                            url: '../index/index'
                          });
                        }, 2000)
                      }
                    }
                  })
                }else{
                  wx.hideLoading();
                  wx.showToast({
                    title: '登录失败,请重新点击授权',
                    content: '请重新点击授权',
                    icon: 'none'
                  })    
                }
              },
              fail: function () {
                wx.hideLoading();
                wx.showToast({
                  title: '网络错误',
                  content: '请重新点击授权',
                  icon: 'none'
                })              
              }
            })
          }
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    app.request({
      url: router.msg.audit,
      method: "GET",    
      success: function (data) {               
        that.setData({
          login: data.login,
          content: data.content,
          tabBar: data.tabbar,
        })      
      }
    });
    if (wx.getStorageSync("uid")){
      wx.login({
        success: function (res) {
          wx.getWeRunData({
            success: function (result) {
              var encryptedData = result.encryptedData;
              var iv = result.iv;
              app.request({
                url: router.user.step,
                method: "POST",
                data: {
                  uid: wx.getStorageSync('uid'),
                  iv: iv,
                  data: encryptedData,
                  code: res.code,
                },
                success: function (data) {                
                  if (!data) {
                    wx.showModal({
                      title: '提示',
                      content: '未关注微信运动，请关注“微信运动”公众号后重试',
                      showCancel: false
                    })
                  }
                }
              })
            },
            fail: function (res) {
              wx.showModal({
                title: '提示',
                content: '未关注微信运动，请关注“微信运动”公众号后重试',
                showCancel: false
              })
            }
          })
        }
      });  
    }   
  },
  isLogin: function (){
    var uid = wx.getStorageSync("uid");
    if (uid){
      this.setData({
        hasUserInfo: true,
        logo: false
      })
    }else{
      this.setData({
        hasUserInfo: false,
        logo: true
      })
    }
  },
  //分享
  onShareAppMessage: function (res) {    
    if (res.from === 'button') {      
      var that = this;
      // 来自页面内转发按钮    
      var id = res.target.dataset.id;
      var index = res.target.dataset.ctr;
      var title = that.data.list[index].name;
      return {
        title: title,
        path: '/pages/index/index',
        success: function (res) {
          app.request({
            url: router.msg.share,
            method: "POST",
            data: {
              id: id,
            },
            success: function (data) {
              if (data) {
                var list = that.data.list;
                var share = list[index].share;
                list[index].share = share + 1;
                that.setData({
                  list: list
                })
                wx.showToast({
                  title: '分享成功',
                  icon: 'success',
                  duration: 2000
                });
              } else {
                wx.showToast({
                  title: '分享失败请重试',
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          });
        },
        fail: function (res) {
          wx.showToast({
            title: '分享已取消',
            icon: 'none',
            duration: 2000
          });
        }
      }
    }
  },
})
